package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.ApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.article.dtos.AuthorDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2022-09-15
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements ApAuthorService {

    /**
     * 保存作者
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveAuthor(AuthorDto dto) {

//        根据app用户id查询作者
        LambdaQueryWrapper<ApAuthor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ApAuthor::getUserId,dto.getUserId());
        ApAuthor apAuthor = getOne(queryWrapper);
        if(apAuthor == null){
            apAuthor = new ApAuthor();
            apAuthor.setUserId(dto.getUserId());
            apAuthor.setWmUserId(dto.getWmUserId());
            apAuthor.setCreatedTime(new Date());
            apAuthor.setName(dto.getName());
            apAuthor.setType(dto.getType());
            save(apAuthor);
            return apAuthor.getId();
        }
        return apAuthor.getId();
    }

    @Override
    public ApAuthor getByWmUserId(Integer wmNewsId) {
//        根据自媒体用户id查询作者
        LambdaQueryWrapper<ApAuthor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ApAuthor::getWmUserId,wmNewsId);
        ApAuthor apAuthor = getOne(queryWrapper);
        return apAuthor;
    }
}
